/*
 * Copyright (c) 2018-2999 九五云信息科技有限公司 All rights reserved.
 *
 * https://www.gz-yami.com/
 *
 * 未经允许，不可做商业用途！
 *
 * 版权所有，侵权必究！
 */

package com.yami.shop.api.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yami.shop.bean.app.dto.ShopHeadInfoDto;
import com.yami.shop.bean.model.ShopDetail;
import com.yami.shop.bean.model.ShopRenovation;
import com.yami.shop.bean.model.UserCollectionShop;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.service.ShopDetailService;
import com.yami.shop.service.ShopRenovationService;
import com.yami.shop.service.UserCollectionShopService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;


/**
 *
 * @author lgh on 2018/08/29.
 */
@RestController
@RequestMapping("/shop")
@Api(tags = "店铺相关接口")
@AllArgsConstructor
public class ShopDetailController {

    private final ShopDetailService shopDetailService;

	private final UserCollectionShopService userCollectionShopService;
	private final ShopRenovationService shopRenovationService;

	@GetMapping("/headInfo")
	@ApiOperation(value = "店铺头部信息", notes = "获取的店铺头部信息")
	public ResponseEntity<ShopHeadInfoDto> getShopHeadInfo(Long shopId) {
		ShopHeadInfoDto shopHeadInfoDto = new ShopHeadInfoDto();
		// 粉丝数量
		int fansCount = userCollectionShopService.count(new LambdaQueryWrapper<UserCollectionShop>().eq(UserCollectionShop::getShopId, shopId));
		ShopRenovation shopRenovation = shopRenovationService.getOne(new LambdaQueryWrapper<ShopRenovation>()
				.eq(ShopRenovation::getShopId,shopId).eq(ShopRenovation::getHomeStatus,1));
		ShopDetail shopDetail = shopDetailService.getShopDetailByShopId(shopId);
		shopHeadInfoDto.setShopStatus(shopDetail.getShopStatus());
		if(!Objects.equals(shopDetail.getShopStatus(),1)){
			return ResponseEntity.ok(shopHeadInfoDto);
		}
		shopHeadInfoDto.setRenovationId(Objects.isNull(shopRenovation) ? null : shopRenovation.getRenovationId());
		shopHeadInfoDto.setShopId(shopId);
		shopHeadInfoDto.setShopLogo(shopDetail.getShopLogo());
		shopHeadInfoDto.setShopName(shopDetail.getShopName());
        shopHeadInfoDto.setIntro(shopDetail.getIntro());
		shopHeadInfoDto.setFansCount(fansCount);
		shopHeadInfoDto.setTel(shopDetail.getTel());
		return ResponseEntity.ok(shopHeadInfoDto);
	}

	@GetMapping("/hotShops")
	@ApiOperation(value = "热门店铺", notes = "热门店铺")
	public ResponseEntity<List<ShopHeadInfoDto>> hotShops() {
		List<ShopHeadInfoDto> hotShopsHead =  shopDetailService.listHotShopsHead();

		return ResponseEntity.ok(hotShopsHead);
	}



	@GetMapping("/searchShops")
	@ApiOperation(value = "搜索店铺", notes = "根据店铺名称搜索店铺")
	public ResponseEntity<Page<ShopHeadInfoDto>> searchShops(PageParam<ShopHeadInfoDto> page,ShopHeadInfoDto shopHeadInfoDto) {
		Page<ShopHeadInfoDto> hotShopsHeadPage =  shopDetailService.searchShops(page,shopHeadInfoDto);

		return ResponseEntity.ok(hotShopsHeadPage);
	}

}
